<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  <meta name="author" content="紫锦木">
  

  <title>vite2使用基本教程 | 紫锦木</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
  <meta name="keywords" content="vue,vite,vue,">
  

  
  <meta name="description" content="vite2 的基本使用教程">

  

  
  <script src="//cdn.jsdelivr.net/npm/leancloud-storage@3.11.1/dist/av-min.js" async></script>
  

  
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
  

  
  <script src="//unpkg.com/valine/dist/Valine.min.js" async></script>
  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"3zwI0gU7grT0MfYUEurqXnWE-gzGzoHsz","appkey":"yVgwglQXfL3vbQiSzeo1Xhqx","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2019-05-01",
    passwords: ["efe07af7441da2b69c4a41e42e73be4db47f66010a56900788a458354a7373ec", ],
    is_post: true,
    lock: false,
    author: "紫锦木",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":true},
    mathjax: true,
    page_type: "",
    root: "/"
  };
</script>

  <script src="/vendor/sha256.min.js"></script>
<script src="/js/auth.js"></script>
<script src="/js/index.js"></script>
<script src="/vendor/qrcode.min.js"></script>

  
  <link rel="icon" href="/images/favicon.ico">
  <link rel="apple-touch-icon" href="/images/touch-icon.png">
  

  <link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

  <link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/styles/components/highlight/highlight.css">

  
</head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/">紫锦木</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 足迹👣</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/" target="_self">首页</a>
      
        <a href="/archives/" target="_self">归档</a>
      
        <a href="/tags/" target="_self">标签</a>
      
        <a href="/categories/" target="_self">分类</a>
      
        <a href="/friends/" target="_self">友链</a>
      
        <a href="/about/" target="_self">关于</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/xfjpeter/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/" target="_self">
            首页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/friends/" target="_self">
            友链
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/about/" target="_self">
            关于
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2021-03-29
    </span>
    
      <span>
        | <a href="/categories/vue/"><i class="fa fa-bookmark"></i>vue</a>
      </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
  </div>
  <h1 class="passage-title">
    vite2使用基本教程
  </h1>
  
  <article class="passage-article">
    <h2 id="1-文档地址"><a href="#1-文档地址" class="headerlink" title="1. 文档地址"></a>1. 文档地址</h2><ul>
<li><a href="https://vitejs.dev/config/" target="_blank" rel="noopener">Vite2 文档地址</a></li>
</ul>
<h2 id="2-创建项目方式"><a href="#2-创建项目方式" class="headerlink" title="2. 创建项目方式"></a>2. 创建项目方式</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm init @vitejs/app</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm init @vitejs/app my-vue-app -- --template vue</span><br></pre></td></tr></table></figure>
<p>##3. 配置文件</p>
<ul>
<li>文件名称：<code>/vite.config.js</code></li>
<li>文档地址：<a href="https://vitejs.dev/config/" target="_blank" rel="noopener">https://vitejs.dev/config/</a></li>
</ul>
<h2 id="4-setup-的语法糖"><a href="#4-setup-的语法糖" class="headerlink" title="4. setup 的语法糖"></a>4. <code>setup</code> 的语法糖</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;img alt=&quot;Vue logo&quot; src=&quot;./assets/logo.png&quot; /&gt;</span><br><span class="line">  &lt;HelloWorld msg=&quot;Hello Vue 3 + Vite&quot; /&gt;</span><br><span class="line">  &lt;Message @myclick=&quot;clickMe&quot; msg=&quot;message from app&quot; ref=&quot;hw&quot; /&gt;</span><br><span class="line">  &lt;Comp /&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script setup&gt;</span><br><span class="line">import HelloWorld from &apos;@/components/HelloWorld.vue&apos;</span><br><span class="line">import Message from &apos;@/components/Message.vue&apos;</span><br><span class="line">import Comp from &apos;@/components/Comp.vue&apos;</span><br><span class="line">import &#123; ref &#125; from &apos;vue&apos;</span><br><span class="line"></span><br><span class="line">const hw = ref(null)</span><br><span class="line"></span><br><span class="line">const clickMe = (options) =&gt; &#123;</span><br><span class="line">  console.log(&apos;我是来自 Message 组件的 Emit 触发的事件，参数是：&apos;, options)</span><br><span class="line">  hw.value.someMethod()</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">#app &#123;</span><br><span class="line">  font-family: Avenir, Helvetica, Arial, sans-serif;</span><br><span class="line">  -webkit-font-smoothing: antialiased;</span><br><span class="line">  -moz-osx-font-smoothing: grayscale;</span><br><span class="line">  text-align: center;</span><br><span class="line">  color: #2c3e50;</span><br><span class="line">  margin-top: 60px;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>
<h2 id="5-获取上下文"><a href="#5-获取上下文" class="headerlink" title="5. 获取上下文"></a>5. 获取上下文</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取上下文</span></span><br><span class="line"><span class="keyword">const</span> ctx = useContext()</span><br><span class="line">ctx.expose(&#123;</span><br><span class="line">  someMethod() &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'expose methods from Message.'</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="6-自定义事件"><a href="#6-自定义事件" class="headerlink" title="6. 自定义事件"></a>6. 自定义事件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义事件</span></span><br><span class="line"><span class="keyword">const</span> emit = defineEmit([<span class="string">'myclick'</span>])</span><br><span class="line"><span class="keyword">const</span> myclick = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// emit('myclick', &#123; type: 'click', message: 'this is message.' &#125;)</span></span><br><span class="line">  ctx.emit(<span class="string">'myclick'</span>, &#123; <span class="attr">type</span>: <span class="string">'click'</span>, <span class="attr">message</span>: <span class="string">'this is message.'</span> &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="7-使用-JSX-语法"><a href="#7-使用-JSX-语法" class="headerlink" title="7. 使用 JSX 语法"></a>7. 使用 <code>JSX</code> 语法</h2><ul>
<li><p>安装插件</p>
<ul>
<li><code>npm i @vitejs/plugin-vue-jsx</code></li>
</ul>
</li>
<li><p>在 <code>vite.config.js</code> 引用插件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> vueJsx <span class="keyword">from</span> <span class="string">'@vitejs/plugin-vue-jsx'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> defineConfig(&#123;</span><br><span class="line">  plugins: [vueJsx()]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  setup() &#123;</span><br><span class="line">    <span class="keyword">const</span> counter = ref(<span class="number">0</span>)</span><br><span class="line">    <span class="keyword">const</span> onclick = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      counter.value++</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="params">()</span> =&gt;</span> (</span><br><span class="line">      &lt;&gt;</span><br><span class="line">        &lt;div&gt;comp&lt;<span class="regexp">/div&gt;</span></span><br><span class="line"><span class="regexp">        &lt;p onClick=&#123;onclick&#125;&gt;&#123;counter.value&#125;&lt;/</span>p&gt;</span><br><span class="line">      &lt;<span class="regexp">/&gt;</span></span><br><span class="line"><span class="regexp">    )</span></span><br><span class="line"><span class="regexp">  &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="8-使用-Mock-数据"><a href="#8-使用-Mock-数据" class="headerlink" title="8. 使用 Mock 数据"></a>8. 使用 <code>Mock</code> 数据</h2><ul>
<li><p>安装插件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i mockjs -S</span><br></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i vite-plugin-mock cross-env -D</span><br></pre></td></tr></table></figure>
</li>
<li><p>更改 <code>package.json</code>文件</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"scripts"</span>: &#123;</span><br><span class="line">    <span class="attr">"dev"</span>: <span class="string">"cross-env NODE_ENV=development vite"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>引入插件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> viteMockServe <span class="keyword">from</span> <span class="string">'vite-plugin-mock'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> defineConfig(&#123;</span><br><span class="line">  plugins: [viteMockServe(&#123; <span class="attr">supportTs</span>: <span class="literal">false</span> &#125;)]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在根目录创建一个 <code>mock</code> 目录，并创建一个 <code>user.js</code> 文件，写入以下数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  url: <span class="string">'/api/getUsers'</span>,</span><br><span class="line">  method: <span class="string">'get'</span>,</span><br><span class="line">  response: <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      code: <span class="number">0</span>,</span><br><span class="line">      message: <span class="string">'ok'</span>,</span><br><span class="line">      data: [<span class="string">'tom'</span>, <span class="string">'jerry'</span>]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>在项目中使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fetch(<span class="string">'/api/getUsers'</span>)</span><br><span class="line">  .then(<span class="function">(<span class="params">res</span>) =&gt;</span> res.json())</span><br><span class="line">  .then(<span class="function">(<span class="params">response</span>) =&gt;</span> <span class="built_in">console</span>.log(response))</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="9-Router-的使用"><a href="#9-Router-的使用" class="headerlink" title="9. Router 的使用"></a>9. Router 的使用</h2><ul>
<li><p>安装插件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i vue-router@next -S</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建 <code>src/router/index.js</code>，写入以下内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createRouter, createWebHashHistory &#125; <span class="keyword">from</span> <span class="string">'vue-router'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> router = createRouter(&#123;</span><br><span class="line">  history: createWebHashHisotry(),</span><br><span class="line">  routes: [</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">'/'</span>, <span class="attr">component</span>: <span class="function"><span class="params">()</span> =&gt;</span> <span class="keyword">import</span>(<span class="string">'@/views/home.vue'</span>) &#125; <span class="comment">// 文件不存在请创建</span></span><br><span class="line">  ]</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router</span><br></pre></td></tr></table></figure>
</li>
<li><p>在 <code>main.js</code> 文件中引入刚刚的文件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> router <span class="keyword">from</span> <span class="string">'./router'</span></span><br><span class="line"></span><br><span class="line">createApp(App).use(router).mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="10-Vuex-的使用"><a href="#10-Vuex-的使用" class="headerlink" title="10. Vuex 的使用"></a>10. Vuex 的使用</h2><ul>
<li><p>安装插件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i vuex@next -S</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建 <code>src/store/index.js</code> ，写入以下内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'vuex'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> createStroe(&#123;</span><br><span class="line">  state: &#123;</span><br><span class="line">    counter: <span class="number">100</span></span><br><span class="line">  &#125;,</span><br><span class="line">  mutations: &#123;</span><br><span class="line">    add(state, payload) &#123;</span><br><span class="line">      state.counter++</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>在 <code>main.js</code> 中使用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span></span><br><span class="line"></span><br><span class="line">createApp(App).use(router).use(store).mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="11-SCSS-的使用"><a href="#11-SCSS-的使用" class="headerlink" title="11. SCSS 的使用"></a>11. SCSS 的使用</h2><ul>
<li><p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i sass -D</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建样式文件 <code>styles/index.scss</code>，写入以下文件</p>
<figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">@<span class="keyword">mixin</span> clearfix &#123;</span><br><span class="line">  &amp;::after &#123;</span><br><span class="line">    <span class="attribute">content</span>: <span class="string">''</span>;</span><br><span class="line">    <span class="attribute">display</span>: table;</span><br><span class="line">    <span class="attribute">clear</span>: both;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@<span class="keyword">mixin</span> scrollBar &#123;</span><br><span class="line">  &amp;::-webkit-scrollbar-track-piece &#123;</span><br><span class="line">    background-color: <span class="number">#d3dce6</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &amp;::-webkit-scrollbar &#123;</span><br><span class="line">    width: <span class="number">6px</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  &amp;::-webkit-scrollbar-thumb &#123;</span><br><span class="line">    background-color: <span class="number">#99a9bf</span>;</span><br><span class="line">    <span class="attribute">border-radius</span>: <span class="number">20px</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>在 <code>main.js</code> 中引入全局样式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="string">'./styles/index.scss'</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="12-集成-Element3"><a href="#12-集成-Element3" class="headerlink" title="12. 集成 Element3"></a>12. 集成 Element3</h2><ul>
<li><p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i element3 -S</span><br></pre></td></tr></table></figure>
</li>
<li><p>在 <code>main.js</code> 中引入</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> element3 <span class="keyword">from</span> <span class="string">'element3'</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">'element3/lib/theme-chalk/index.css'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// use 一下</span></span><br><span class="line">createApp(App).use(element3).mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>另外一种方式，创建 <code>plugins/element3.js</code> 文件，写入以下内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> element3 <span class="keyword">from</span> <span class="string">'element3'</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">'element3/lib/theme-chalk/index.css'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params">app</span>) </span>&#123;</span><br><span class="line">  app.use(element3)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 在 main.js 中引入</span></span><br><span class="line"><span class="keyword">import</span> element3 <span class="string">'./plugins/element3.js'</span></span><br><span class="line">createApp(App).use(element3).mount(<span class="string">'#app'</span>)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="13-配合-github-自动部署"><a href="#13-配合-github-自动部署" class="headerlink" title="13. 配合 github 自动部署"></a>13. 配合 github 自动部署</h2><ul>
<li><p>项目根目录创建 <code>.github/workflows/publish.yaml</code> 文件，写入以下内容</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 给工作流设置名字</span></span><br><span class="line"><span class="attr">name:</span> <span class="string">打包应用并上传</span></span><br><span class="line"><span class="attr">on:</span></span><br><span class="line">	<span class="comment"># 工作流监听的事件</span></span><br><span class="line"><span class="attr">  push:</span></span><br><span class="line">  	<span class="comment"># 监听的分支列表</span></span><br><span class="line"><span class="attr">    branches:</span></span><br><span class="line"><span class="bullet">      -</span> <span class="string">master</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 工作流</span></span><br><span class="line"><span class="attr">jobs:</span></span><br><span class="line"><span class="attr">  build:</span></span><br><span class="line">     <span class="comment"># runs-on 指定 job 任务运行所需要的虚拟机环境</span></span><br><span class="line"><span class="attr">    runs-on:</span> <span class="string">ubuntu-latest</span></span><br><span class="line"><span class="attr">    steps:</span></span><br><span class="line">      <span class="comment"># 获取源码</span></span><br><span class="line"><span class="attr">      - name:</span> <span class="string">迁出代码</span></span><br><span class="line"><span class="attr">        uses:</span> <span class="string">actions/checkout@master</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># 安装 node10</span></span><br><span class="line"><span class="attr">      - name:</span> <span class="string">安装</span> <span class="string">node.js</span></span><br><span class="line"><span class="attr">        uses:</span> <span class="string">actions/setup-node@v1</span></span><br><span class="line"><span class="attr">        with:</span></span><br><span class="line"><span class="attr">          node-version:</span> <span class="number">14.0</span><span class="number">.0</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># 安装依赖</span></span><br><span class="line"><span class="attr">      - name:</span> <span class="string">安装依赖</span></span><br><span class="line"><span class="attr">        run:</span> <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># 打包</span></span><br><span class="line"><span class="attr">      - name:</span> <span class="string">打包</span></span><br><span class="line"><span class="attr">        run:</span> <span class="string">npm</span> <span class="string">run</span> <span class="string">build</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># 上传 阿里云</span></span><br><span class="line"><span class="attr">      - name:</span> <span class="string">发布到阿里云</span></span><br><span class="line"><span class="attr">        uses:</span> <span class="string">easingthemes/ssh-deploy@v2.1.5</span></span><br><span class="line"><span class="attr">        env:</span></span><br><span class="line">          <span class="comment"># 私钥</span></span><br><span class="line"><span class="attr">          SSH_PRIVATE_KEY:</span> <span class="string">$&#123;&#123;</span> <span class="string">secrets.PRIVATE_KEY</span> <span class="string">&#125;&#125;</span></span><br><span class="line">          <span class="comment"># scp 参数</span></span><br><span class="line"><span class="attr">          ARGS:</span> <span class="string">"-avzr --delete"</span></span><br><span class="line">          <span class="comment"># 源目录</span></span><br><span class="line"><span class="attr">          SOURCE:</span> <span class="string">"dist"</span></span><br><span class="line">          <span class="comment"># 服务器 IP 地址</span></span><br><span class="line"><span class="attr">          REMOTE_HOST:</span> <span class="string">"47.93.230.33"</span></span><br><span class="line">          <span class="comment"># 用户</span></span><br><span class="line"><span class="attr">          REMOTE_USER:</span> <span class="string">"root"</span></span><br><span class="line">          <span class="comment"># 目标地址</span></span><br><span class="line"><span class="attr">          TARGET:</span> <span class="string">"/www/wwwroot/vue-in-action"</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>创建 <code>RSA</code> 证书免密登陆</p>
<ul>
<li><p>输入以下命令创建</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -m PEM -t rsa -b 4096</span><br></pre></td></tr></table></figure>
</li>
<li><p>找到刚刚创建的 <code>~/.ssh/id_rsa.pub</code> 文件，复制到服务器的 <code>~/.ssh/authorized_keys</code> 文件中，实现免密登陆</p>
</li>
</ul>
</li>
<li><p>在 <code>github</code> 指定的项目 <code>settings/Secrets</code> 中添加一个名为 <code>PRIVATE_KEY</code> 的变量，值为刚刚创建的 <code>~/.ssh/id_rsa</code> 的内容</p>
</li>
<li><p>执行以上步骤之后，每次 <code>push</code> 代码到 <code>master</code> 分支的时候都会执行 <code>build</code> 这个任务</p>
</li>
</ul>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-文档地址"><span class="toc-text">1. 文档地址</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-创建项目方式"><span class="toc-text">2. 创建项目方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-setup-的语法糖"><span class="toc-text">4. setup 的语法糖</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-获取上下文"><span class="toc-text">5. 获取上下文</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-自定义事件"><span class="toc-text">6. 自定义事件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-使用-JSX-语法"><span class="toc-text">7. 使用 JSX 语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-使用-Mock-数据"><span class="toc-text">8. 使用 Mock 数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-Router-的使用"><span class="toc-text">9. Router 的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-Vuex-的使用"><span class="toc-text">10. Vuex 的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-SCSS-的使用"><span class="toc-text">11. SCSS 的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-集成-Element3"><span class="toc-text">12. 集成 Element3</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-配合-github-自动部署"><span class="toc-text">13. 配合 github 自动部署</span></a></li></ol>
  </div>
</aside>
  
    <aside class="passage-copyright">
      <div>本文作者: 紫锦木</div>
      
        <div>
          原文链接: 
          <a href target="_blank">https://www.johnxu.net/articles/ckmumqlwk000ocm8oc2lqtp0m.html</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/tags/vite/"><i class="fa fa-tags"></i>vite</a>
     
      <a href="/tags/vue/"><i class="fa fa-tags"></i>vue</a>
    
    </div>
  
</div>

    </main>
    
      
<div class="site-comment-contanier" data-plateform="leancloud">
  
    <p id="site-comment-info">
      <i class="fa fa-spinner fa-spin"></i> 评论加载中
    </p>
    <div id="site-comment"></div>
  
</div>
    
    <div class="site-footer-wrapper">
  <footer class="site-footer">
    
    
    <div class="site-footer-col">
      <h5 class="site-footer-title">博客推荐</h5>
      
      <span class="site-footer-item">
        <a href="https://godbmw.com/" target="_blank">GodBMW</a>
      </span>
      
      <span class="site-footer-item">
        <a href="http://ruanyifeng.com/" target="_blank">阮一峰的个人网站</a>
      </span>
      
    </div>
    
    <div class="site-footer-col">
      <h5 class="site-footer-title">抓到我</h5>
      
      <span class="site-footer-item">
        <a href="https://juejin.im/user/5b4ffe335188251afa62cd8a" target="_blank">掘金</a>
      </span>
      
      <span class="site-footer-item">
        <a href="https://segmentfault.com/u/johnxu_58f42b627331b" target="_blank">思否</a>
      </span>
      
      <span class="site-footer-item">
        <a href="https://www.jianshu.com/u/56f6d0e3bae3" target="_blank">简书</a>
      </span>
      
    </div>
    
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
    <div class="site-footer-info">
      <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
    </div>
    
    
    <div class="site-footer-info">
      <i class="fa fa-at"></i> Email: fsyzxz@163.com
    </div>
    
    <div class="site-footer-info">
      <i class="fa fa-copyright"></i>
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div>
    <div class="site-footer-info">
      <a href="http://www.beian.miit.gov.cn" target="_blank">蜀ICP备17000668号</a>
    </div>
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">
  <div class="bottom-bar-left">
    <a href="javascript:void(0);" data-enable="false">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/articles/ckmumqlw60005cm8og5fegq9p.html" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
      <a href="javascript:void(0);" id="site-reward">
        <i class="fa fa-thumbs-up"></i>
      </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
    <a id="share-btn-wechat" href="javascript:void(0);" target="_blank">
      <i class="fa fa-wechat"></i>
    </a>
  
</div>
    


  <script async>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>




    
  </body>
</html>